#include "StdAfx.h"
#include "OptionUI.h"
#include "BlueRenderEngineUI.h"

COptionUI::COptionUI(void): m_cxWidth(0), m_uButtonState(0), m_uStyle(DT_LEFT), m_bSelected(false)
{
}

COptionUI::~COptionUI(void)
{
}

LPCTSTR COptionUI::GetClass() const
{
	return _T("OptionUI");
}

UINT COptionUI::GetControlFlags() const
{
	return UIFLAG_TABSTOP;
}

void COptionUI::Event(TEventUI& event)
{
	if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK )
	{
		if( ::PtInRect(&m_rcItem, event.ptMouse) && IsEnabled() ) {
			m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED;
			Invalidate();
		}
	}
	if( event.Type == UIEVENT_MOUSEMOVE )
	{
		if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
			if( ::PtInRect(&m_rcItem, event.ptMouse) ) m_uButtonState |= UISTATE_PUSHED;
			else m_uButtonState &= ~UISTATE_PUSHED;
			Invalidate();
		}
	}
	if( event.Type == UIEVENT_BUTTONUP )
	{
		if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
			if( ::PtInRect(&m_rcItem, event.ptMouse) ) Activate();
			m_uButtonState &= ~(UISTATE_PUSHED | UISTATE_CAPTURED);
			Invalidate();
		}
	}
	CControlUI::Event(event);
}

bool COptionUI::IsChecked() const
{
	return m_bSelected;
}

void COptionUI::SetCheck(bool bSelected)
{
	if( m_bSelected == bSelected ) return;
	m_bSelected = bSelected;
	if( m_pManager != NULL ) m_pManager->SendNotify(this, _T("changed"));
	Invalidate();
}

bool COptionUI::Activate()
{
	if( !CControlUI::Activate() ) return false;
	SetCheck(true);
	return true;
}

void COptionUI::SetWidth(int cxWidth)
{
	m_cxWidth = cxWidth;
	UpdateLayout();
}

void COptionUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if( _tcscmp(pstrName, _T("width")) == 0 ) SetWidth(_ttoi(pstrValue));
	else if( _tcscmp(pstrName, _T("selected")) == 0 ) SetCheck(_tcscmp(pstrValue, _T("true")) == 0);
	else if( _tcscmp(pstrName, _T("align")) == 0 ) {
		if( _tcsstr(pstrValue, _T("right")) != NULL ) m_uStyle |= DT_RIGHT;
	}
	else CControlUI::SetAttribute(pstrName, pstrValue);
}

SIZE COptionUI::EstimateSize(SIZE /*szAvailable*/)
{
	return CStdSize(m_cxWidth, 18 + m_pManager->GetThemeFontInfo(UIFONT_NORMAL).tmHeight);
}

void COptionUI::DoPaint(HDC hDC, const RECT& rcPaint)
{
	// Draw button
	UINT uState = 0;
	if( m_bSelected ) uState |= UISTATE_CHECKED;
	if( IsFocused() ) uState |= UISTATE_FOCUSED;
	if( !IsEnabled() ) uState |= UISTATE_DISABLED;
	CBlueRenderEngineUI::DoPaintOptionBox(hDC, m_pManager, m_rcItem, m_sText, m_uButtonState | uState, m_uStyle);
}

